<template>
    <div class="common jobAdd">
        <innerCommon 
            :location="location"
        >
            <template slot="left">
                <span>{{type==0?'新增':'修改'}}</span>
            </template>
            <div slot="right">
                <a-button type="primary" @click="returnBack()">返回</a-button>
            </div>
            <div slot="table">
                <eform
                    v-if="fields||type==0"
                    :formData="formData"
                    class="form"
                    ref="form"
                    :fields="fields"
                >
                    <div slot="institute">
                        <a-select 
                        style="width:100%;margin-right:10px;"  
                        allow-clear 
                        placeholder="请选择单位名称"
                        v-decorator="['instituteId',{rules: [{ required: true, message: '请选择单位名称' }]}]"
                        >
                            <a-select-option :value="item.id" v-for="(item,index) in instituteList" :key="index">
                                {{item.name}}
                            </a-select-option>
                        </a-select>
                    </div>
                    <div slot="wordAddress">
                        <eAddress
                            v-decorator="['addressValue',{rules: [{ required: true, message: '请选择地址' }]}]"
                            :defaultAddress='defaultAddress'
                            @changeAddress="changeAddress"
                        ></eAddress>
                    </div>
                    <div slot="education">
                        <a-select 
                        style="width:100%;margin-right:10px;"  
                        allow-clear 
                        placeholder="请选择学历类型"
                        v-decorator="['education',{rules: [{ required: true, message: '请选择学历类型' }]}]"
                        >
                            <a-select-option :value="item" v-for="(item,index) in educationList" :key="index">
                                {{item}}
                            </a-select-option>
                        </a-select>
                    </div>
                    <div slot="content">
                        <eQuill
                            @getQuillContent="getQuillContent"
                            v-decorator="['content',{rules: [{ required: true, message: '请输入内容' }]}]"
                            :defaultData="quillInfo"
                        ></eQuill>
                    </div>
                </eform>
                <div class="submitBtn">
                    <a-button @click="returnBack()">取消</a-button>
                    <a-button type="primary" @click="save()">保存</a-button>
                </div>
            </div>
        </innerCommon>
    </div>
</template>
<script>
import innerCommon from "@/components/common/innerCommon"
import eform from "@/components/common/form"
import {getCommonAllianceListApi,addJobApi,getInstituteDetailApi,editJobApi} from "@/request/api"
import eAddress from "@/components/common/eAddress"
import eQuill from "@/components/common/eQuill"
export default {
    name:"jobAdd",
    components:{innerCommon,eform,eAddress,eQuill},
    created(){
        this.jobId=this.$route.query.id
        this.type=this.jobId?1:0
        this.location=[{
            name:"信息发布管理",
            path:''
        },{
            name:"人才需求",
            path:'/job'
        },{
            name:this.type==0?"新增":'修改',
            path:'/jobAdd'
        }]
        this.getToken(getCommonAllianceListApi,{},res=>{
            this.instituteList=res.data
            if(this.type==1){
                this.getToken(getInstituteDetailApi,{'jobId':this.jobId},res=>{
                    this.fields={...res.data,'instituteId':res.data.institute.id,'addressValue':[res.data.province||'',res.data.city||'']}
                    this.quillInfo=res.data.content
                    this.defaultAddress=[res.data.province||'',res.data.city||'']
                })  
            }
        })
    },
    
    data(){
        let validatorNumber=(rule,value,callback)=>{
            let reg=/^[0-9]*$/
            if(reg.test(value)){
                callback()
            }else{
                callback(new Error('必须是数字'))
            }
        }
        const formData=[{
            label:"单位名称",
            scopedSlots: { customRender: 'institute'},
        },{
            label:"职位名称",
            prop:"title",
            placeholder:'请输入职位名称',
            rules:[{required:true,message:'请输入职位名称'}],
        },{
            label:"职位类别",
            prop:"type",
            placeholder:'请输入职位类别',
            rules:[{required:true,message:'请输入职位类别'}],
        },{
            label:"招聘人数",
            prop:"needNum",
            placeholder:'请输入招聘人数',
            rules:[{required:true,message:'请输入招聘人数'},{validator:validatorNumber}],
        },{
            label:"学历要求",
            scopedSlots: { customRender: 'education'},
        },{
            label:"薪资待遇",
            prop:"salary",
            placeholder:'请输入薪资待遇',
            rules:[{required:true,message:'请输入薪资待遇'},{validator:validatorNumber}],
        },{
            label:"工作地点",
            scopedSlots: { customRender: 'wordAddress'},
        },{
            label:"详细内容",
            scopedSlots: { customRender: 'content'},
        }]
        
        return{
            validatorNumber,
            location:[],
            type:0,
            fields:null,
            formData,
            educationList:['不限','高中','技校','中专','大专','本科','硕士','博士'],
            instituteList:[],
            quillInfo:'',
            defaultAddress:'',
            jobId:'',
        }
    },
    methods:{
        returnBack(){
            this.$confirm({
                title: '确认返回吗?',
                cancelText:"取消",
                okText:'确定',
                onOk:()=>{
                    this.$router.go(-1)
                },
                onCancel() {
                },
            });
        },
        getQuillContent(data){
            this.quillInfo=data
            this.$refs.form.form.setFieldsValue({['content']:data})
        },
        changeAddress(data){
            this.$refs.form.form.setFieldsValue({['addressValue']:data})
        },
        save(){
            this.$refs.form.form.validateFields().then(res=>{
                let formData
                formData={...res,'province':res.addressValue[0],'city':res.addressValue[1]}
                this.getToken(this.type==0?addJobApi:editJobApi,this.type==0?formData:{...formData,'jobId':this.jobId},res=>{
                    this.$message.success(res.message)
                    this.$router.go(-1)
                })
                
                
            })
        }
    }
}
</script>